package com.zhiwang.planet.util

import android.app.Activity
import android.content.Context
import android.graphics.BitmapFactory
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.tauth.Tencent
import java.io.File
import android.os.Bundle
import com.zhiwang.planet.mvvm.model.entity.ShareInformation
import com.tencent.connect.share.QQShare
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import com.tencent.tauth.IUiListener
import com.tencent.tauth.UiError
import com.zhiwang.planet.util.Store.Store.APP_ID


class ShareHelper {

    companion object {
        fun create(context: Context): ShareHelper {
            return ShareHelper(context)
        }

    }

    private var context: Context

    private var api: IWXAPI

    constructor(context: Context) {
        this.context = context
        api = WXAPIFactory.createWXAPI(context, APP_ID, false);
    }


    fun shareImgeToWxFriend(shareInfo: ShareInformation) {
        val file = File(shareInfo.imgLocalPath)
        if (!file.exists()) {
            Toaster.message("分享资源暂不存在")
            return
        }

        val webpage = WXWebpageObject()
        webpage.webpageUrl = shareInfo.url

        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        val msg = WXMediaMessage(webpage)
        msg.title = shareInfo.title
        msg.description = shareInfo.summary
        val thumbBmp = BitmapFactory.decodeFile(shareInfo.imgLocalPath)
        msg.thumbData = BitmapUtil.bmpToByteArray(thumbBmp, true)

//        val bmp = BitmapFactory.decodeFile(shareInfo.imgLocalPath)
//        val thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true)
//        bmp.recycle()
//        msg.thumbData = BitmapUtil.bmpToByteArray(thumbBmp, true)

        val req = SendMessageToWX.Req()
        req.transaction = buildTransaction("img")
        req.message = msg
        req.scene = SendMessageToWX.Req.WXSceneSession
        api.sendReq(req)
    }

    fun shareImgeToWxTimeLine(shareInfo: ShareInformation) {
        val file = File(shareInfo.imgLocalPath)
        if (!file.exists()) {
            Toaster.message("分享资源暂不存在")
            return
        }

        val webpage = WXWebpageObject()
        webpage.webpageUrl = shareInfo.url

        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        val msg = WXMediaMessage(webpage)
        msg.title = shareInfo.title
        msg.description = shareInfo.summary
        val thumbBmp = BitmapFactory.decodeFile(shareInfo.imgLocalPath)
        msg.thumbData = BitmapUtil.bmpToByteArray(thumbBmp, true)

        //构造一个Req
        val req = SendMessageToWX.Req()
        req.transaction = buildTransaction("webpage")
        req.message = msg
        req.scene = SendMessageToWX.Req.WXSceneTimeline
        //调用api接口，发送数据到微信
        api.sendReq(req)
    }

    private fun buildTransaction(type: String?): String {
        return if (type == null) System.currentTimeMillis().toString() else type + System.currentTimeMillis()
    }

    fun shareImageToQQFriend(activity: Activity, sharePath: String, link: String = "") {
        var tencent = Tencent.createInstance("1108013988", context)
        val bundle = Bundle()
        //这条分享消息被好友点击后的跳转URL。
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, link)
        //分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_SUMMARY不能全为空，最少必须有一个是有值的。
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "您有一个10元红包，赶紧领取！")
        //分享的图片URL
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, sharePath)
        //分享的消息摘要，最长50个字
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "趣味星球,边玩边赚，0成本，提现快，绝对靠谱！")
        //手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "趣味星球")
        //标识该消息的来源应用，值为应用名称+AppId。
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT)
        tencent.shareToQQ(activity, bundle, object : IUiListener {
            override fun onComplete(p0: Any?) {
                Toaster.message("分享成功")
            }

            override fun onCancel() {
            }

            override fun onError(p0: UiError?) {
            }

        })
    }

    fun shareImageToQQZone(activity: Activity, path: String, link: String = "") {
        var tencent = Tencent.createInstance("1108013988", context)
        val bundle = Bundle()
        //这条分享消息被好友点击后的跳转URL。
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, link)
        //分享的标题。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_SUMMARY不能全为空，最少必须有一个是有值的。
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "您有一个10元红包，赶紧领取！")
        //分享的图片URL
        bundle.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, arrayListOf(path))
        //分享的消息摘要，最长50个字
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "趣味星球,边玩边赚，0成本，提现快，绝对靠谱！")
        //手Q客户端顶部，替换“返回”按钮文字，如果为空，用返回代替
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "趣味星球")
        //标识该消息的来源应用，值为应用名称+AppId。
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT)
        tencent.shareToQzone(activity, bundle, object : IUiListener {
            override fun onComplete(p0: Any?) {
                Toaster.message("分享成功")
            }

            override fun onCancel() {
                Toaster.message("分享取消")

            }

            override fun onError(p0: UiError?) {
                p0?.errorMessage?.let { Toaster.message(it) }
            }

        })
    }

}